<template>
  <div class="header">
    <!-- 上一部分 -->
    <div class="topBar">
      <div class="container">
        <!-- 左四小格 -->
        <div class="container_menu">
          <a href="javascript:;">小米商城</a>
          <a href="javascript:;">MUI</a>
          <a href="javascript:;">云服务</a>
          <a href="javascript:;">协议规则</a>
        </div>
        <!-- 右边使用者 -->
        <div class="container_user">
          <a href="javascript:;" v-if="username">{{ username }}</a>
          <a href="javascript:;" v-if="!username" @click="login">登录</a>
          <a href="javascript:;" v-if="username" @click="logout">退出</a>
          <a href="/order/list" v-if="username">我的订单</a>
          <a href="javascript:;" class="cart"
            ><span class="icon-cart" @click="gotoCart"></span>购物车({{
              $store.state.cartCount
            }})</a
          >
        </div>
      </div>
    </div>
    <!-- 下一部分 -->
    <div class="navHeader">
      <div class="container">
        <div class="header-logo">
          <a href="/index"></a>
        </div>
        <div class="header-menu">
          <div class="item-menu">
            <span>小米手机</span>
            <div class="children">
              <ul>
                <li
                  class="product"
                  v-for="(item, index) in phoneList"
                  :key="index"
                >
                  <a :href="'/product' + item.id" target="_blank">
                    <div class="product-img">
                      <img :src="item.mainImage" alt="" />
                    </div>
                    <div class="product-name">{{ item.name }}</div>
                    <div class="product-price">{{ item.price | currency }}</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="item-menu">
            <span>红米手机</span>
            <div class="children"></div>
          </div>
          <div class="item-menu">
            <span>小米电视</span>
            <div class="children"></div>
          </div>
        </div>
        <div class="header-search">
          <div class="wrapper">
            <input type="text" name="keyword" />
            <a href="javascript:;"></a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      phoneList: [],
    };
  },
  computed: {
    // 如果不用computed 的页面刷新就更新不了，因为我们这个数据要去后台数据库中请求的，如果一刷新没有拿到，后期有了也不会刷新，用这个就会随时有新值就会立马覆盖渲染，就不会有延迟，数据也不错
    username() {
      return this.$store.state.username; //这边基于同步更新，无论哪个用户登录我们都能实时更新他的账户名
    },
    cartCount() {
      return this.$store.state.cartCount; //这边基于同步更新，无论哪个用户登录我们都能实时更新他的数量
    },
    //第二种方法
    // ...mapState(["username", "cartCount"]),
  },
  filters: {
    currency(val) {
      if (!val) return "0.00";
      return "￥" + val.toFixed(2) + "元";
    },
  },
  methods: {
    getProductList() {
      //调用数据
      this.axios
        .get("/products", { params: { categoryId: "100012" } })
        .then((res) => {
          if (res.list.length > 6) {
            this.phoneList = res.list.slice(0, 6);
          }
        });
    },
    login() {
      //跳转登录路由
      this.$router.push("/login");
    },
    //退出登录
    logout() {
      this.axios.post("/user/logout").then(() => {
        this.$message.success("退出成功");
        this.$cookie.set("userId", "", { expires: "-1" }); //关闭的窗口的时候userid失效
        this.$store.dispatch("saveUserName", ""); //退出时取消vuex中的saveUserName，用户名
        this.$store.dispatch("saveCartCount", "0"); //退出时取消vuex中的saveCartCount，购物车数量
      });
    },
    //重新拿取一次枸购物车的数据，要不然退出再登录时，购物车的数量又不对了，
    getCartCount() {
      this.axios.get("/carts/products/sum").then((res = 0) => {
        //to-do 保存到vuex中
        this.$store.dispatch("saveCartCount", res);
      });
    },
    gotoCart() {
      //跳转购物路由
      this.$router.push("/cart");
    },
  },
  mounted() {
    this.getProductList();
    let params = this.$route.params;
    //只有从登陆页面跳转过来的时候才会获取购物车数量
    if (params && params.from == "login") {
      this.getCartCount();
    }
  },
};
</script>

<style lang="scss">
@import "../assets/scss/base";
@import "../assets/scss/mixin";
@import "../assets/scss/config";
.header {
  .topBar {
    height: 39px;
    line-height: 39px;
    background-color: #333333;
    color: #b0b0b0;
    .container {
      @include flex();
      a {
        display: inline-block;
        color: #b0b0b0;
        margin-right: 17px;
      }
      .cart {
        width: 110px;
        text-align: center;
        background-color: #ff6600;
        color: #ffffff;
        margin-right: 0px;
        .icon-cart {
          @include bgImg(16px, 12px, "../../public/imgs/icon-cart-checked.png");
          margin-right: 4px;
        }
      }
    }
  }
  .navHeader {
    .container {
      position: relative;
      height: 112px;
      @include flex();
      .header-logo {
        display: inline-block;
        width: 55px;
        height: 55px;
        background-color: #ff6600;

        a {
          display: inline-block;
          width: 110px;
          &:before {
            content: " ";
            @include bgImg(55px, 55px, "../../public/imgs/mi-logo.png");
            transition: margin 0.2s;
          }
          &:after {
            content: " ";
            @include bgImg(55px, 55px, "../../public/imgs/mi-home.png");
          }
          &:hover:before {
            margin-left: -55px;
            transition: margin 0.2s;
          }
        }
      }
      .header-menu {
        display: inline-block;
        width: 643px;
        padding-left: 209px;
        .item-menu {
          display: inline-block;
          color: #333333;
          font-weight: bold;
          font-size: 16px;
          line-height: 112px;
          margin-right: 20px;
          span {
            cursor: pointer;
          }
          &:hover {
            color: $colorA;
            .children {
              height: 220px;
              opacity: 1;
            }
          }
          .children {
            position: absolute;
            left: 0;
            top: 112px;
            height: 0px;
            opacity: 0;
            width: 1226px;
            transition: all 0.5s;
            overflow: hidden;
            border-top: 1px solid #e5e5e5;
            box-shadow: 0px, 7px, 6px, 0px rgba(0, 0, 0, 0.11);
          }
          a {
            display: inline-block;
          }
          .product {
            position: relative;
            float: left;
            width: 16.6%;
            height: 220px;
            font-size: 12px;
            line-height: 12px;
            text-align: center;
            background-color: #ffffff;
            z-index: 10;
            &:before {
              content: " ";
              position: absolute;
              top: 28px;
              right: 0px;
              border-right: 1px solid $colorF;
              height: 100px;
              width: 1px;
            }
            &:last-child:before {
              display: none;
            }
            .img {
              height: 112px;
              width: auto;
              margin-top: 26px;
            }
            .product-img {
              height: 112px;
            }
            .product-name {
              font-weight: bold;
              margin: 19px 0px 8px 0px;
              color: $colorB;
            }
            .product-price {
              color: $colorA;
            }
          }
        }
      }
      .header-search {
        width: 319px;
        .wrapper {
          border: 1px solid #e0e0e0;
          //   border: none;
          height: 50px;
          display: flex;
          align-items: center;
        }
        input {
          border: none;
          border-right: 1px solid #e0e0e0;
          width: 264px;
          height: 50px;
          padding-left: 14px;
          border: none;
          box-sizing: border-box;
        }
        a {
          @include bgImg(18px, 18px, "../../public/imgs/icon-search.png");
          margin-left: 17px;
        }
      }
    }
  }
}
</style>
